<template>
  <div class="home-page container-md">
    <section class="text-center container">
      <div class="row py-lg-5">
        <div class="col-lg-6 col-md-8 mx-auto">
          <img src="../assets/callout.svg" alt="callout" class="w-50"/>
          <h2 class="font-weight-light">随心写作，自由表达</h2>
          <p>
            <router-link to="/create" class="btn btn-primary my-2">开始写文章</router-link>
          </p>
        </div>
      </div>
    </section>
    <h4 class="font-weight-bold text-center mb-4">发现精彩</h4>
    <column-list :list="columns"></column-list>
    <div class="d-grid gap-2 col-3 mx-auto">
      <button
        class="btn btn-outline-primary mt-2 mb-5"
        @click="loadMorePage" v-if="!isLastPage"
      >
        加载更多
      </button>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { GlobalDataProps } from '../store'
import useLoadMore from '../hooks/useLoadMore'
import ColumnList from '../components/ColumnList.vue'

export default defineComponent({
  name: 'Home',
  components: {
    ColumnList
  },
  setup () {
    const store = useStore<GlobalDataProps>()
    const totalColumns = computed(() => store.state.columns.total || 0)
    const currentPage = computed(() => store.state.columns.currentPage || 0)
    // 组件挂载的时候获取所有专栏信息
    onMounted(() => {
      store.dispatch('fetchColumns')
    })
    // vuex中store存储的数据是响应式的，所有需要计算属性获取数据，保持响应性
    const columns = computed(() => store.getters.getColumns)
    // currentPage存在值就加1，没有值就赋初值2
    const { loadMorePage, isLastPage } = useLoadMore('fetchColumns', totalColumns, { currentPage: currentPage.value }, 6)
    return {
      columns,
      loadMorePage,
      isLastPage
    }
  }
})
</script>
